package com.keqiang.chart.impl.radar.impl

import android.graphics.RectF
import android.view.MotionEvent
import com.keqiang.chart.impl.radar.BaseTouchHandler
import me.zhouzhuo810.magpiex.utils.SimpleUtil

/**
 * 处理雷达图的触摸
 *
 * @author Created by wanggaowan on 2022/10/31 09:46
 */
open class RadarTouchHandler(chart: RadarChart) : BaseTouchHandler<RadarChart, List<RadarEntity>>(chart) {

    override fun findClickData(e: MotionEvent): List<RadarEntity>? {
        if (chart.data.isNullOrEmpty()) {
            return null
        }

        val inRangePoint = mutableListOf<RadarEntity>()
        chart.data?.forEach {
            for (value in it.values) {
                if (isTouchInRectF(e.x, e.y, value.drawRectF)) {
                    inRangePoint.add(value)
                    break
                }
            }
        }

        return if (inRangePoint.isEmpty()) null else inRangePoint
    }

    /**
     * 点击是否在指定范围内容
     */
    private fun isTouchInRectF(x: Float, y: Float, rectF: RectF): Boolean {
        val validRange = SimpleUtil.getScaledValue(30)
        if (x >= rectF.left - validRange && x <= rectF.left + validRange) {
            return y >= rectF.top - validRange && y <= rectF.top + validRange
        }

        return false
    }
}